﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TicketingSystem;


namespace TicketingSystemAsp
{
    public partial class Categories : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.CategoriesListView.DataSource = TicketingEnchancements.GetCategories();
                this.DataBind();
            }
        }

        protected void CategoryEdit(object sender, ListViewEditEventArgs args)
        {
            this.CategoriesListView.EditIndex = args.NewEditIndex;
            BindCategoryView();
        }

        private void BindCategoryView()
        {
            this.CategoriesListView.DataSource = TicketingEnchancements.GetCategories();
            this.DataBind();
        }

        protected void UpdateCategory(object sender, ListViewUpdateEventArgs args)
        {
            string name = (CategoriesListView.Items[args.ItemIndex].FindControl("CategoryNameTextBox") as TextBox).Text;
            int id = int.Parse((CategoriesListView.Items[args.ItemIndex].FindControl("IDLabel") as Label).Text);

            TicketingEnchancements.UpdateCategory(id, new Category() { CategoryName = name });

            this.CategoriesListView.EditIndex = -1;
            BindCategoryView();
        }

        protected void DeleteCategory(object sender, ListViewDeleteEventArgs args)
        {
            int id = int.Parse((CategoriesListView.Items[args.ItemIndex].FindControl("IDLabel1") as Label).Text);

            TicketingEnchancements.DeleteCategory(id);

            BindCategoryView();
        }

        protected void AddCategory(object sender, ListViewInsertEventArgs args)
        {
            string newCategory = (args.Item.FindControl("InsertItemTextBox") as TextBox).Text;
            if (string.IsNullOrEmpty(newCategory))
            {
                return;
            }


            TicketingEnchancements.AddCatiegory(new Category() { CategoryName = newCategory });

            BindCategoryView();
        }
    }
}